package com.studentachievement.servlets;
import com.studentachievement.beans.Admin;
import com.studentachievement.beans.Score;
import com.studentachievement.beans.Student;
import com.studentachievement.dao.AdminDAO;
import com.studentachievement.dao.StudentDAO;
import com.studentachievement.utils.DatabaseUtil;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@WebServlet("/StudentHomeServlet")
public class StudentHomeServlet extends HttpServlet {
    private static final Logger logger = Logger.getLogger(StudentHomeServlet.class.getName());
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //从会话中获取登录者信息
        HttpSession session = request.getSession(false); // false 表示如果没有session则返回null
        if (session != null) {
            Student student = (Student) session.getAttribute("student");
            // 在此处理 admin 对象
            if (student != null) {
                // 获取学生的成绩
                List<Score> scores = StudentDAO.getStudentScores(student.getStudentId());
                // 将成绩存入 request 中，传递给 JSP 页面
                request.setAttribute("scores", scores);

                // 使用 RequestDispatcher 转发到 JSP 页面
                RequestDispatcher dispatcher = request.getRequestDispatcher("student_home.jsp");
                try {
                    dispatcher.forward(request, response);
                } catch (ServletException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
            else {
                // 没有登录的情况，重定向到登录页面
                RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
                try {
                    dispatcher.forward(request, response);
                } catch (ServletException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        else {
            // 没有登录的情况，重定向到登录页面
            // 没有登录的情况，重定向到登录页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
            try {
                dispatcher.forward(request, response);
            } catch (ServletException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        doGet(request, response);
    }
}
